<?php
class ReviewsController extends AppController {

	var $name = 'Reviews';
	var $uses = array('Review', 'Listing');
	
	var $paginate = array(
		'Review' => array(
	        'limit' => 10,
	        'order' => array(
	            'Review.id' => 'DESC'
	        )
		)
    );
	
	/**
	 * Review Model
	 *
	 * @var Review
	 */
	var $Review;
	
	/**
	 * Tyk zadavame spisaka na action-ite s publichen dostyp (action-ite bez authorizaciq)
	 */
	function __initPublicActions() {
		$this->Auth->allow(array(
			'init_db',
			'index',
			'add',
			'edit',
			'view',
			'delete'
		));
	}
	
	function beforeFilter() {
        parent::beforeFilter();
        
        $this->__initPublicActions();
    }
    
    function init_db() {
    	$this->loadModel('Group');
    	
    	$group =& $this->Group;
    	
    	//------------- root [id=1]
    	$group->id = 1;
    	
    	$this->Acl->allow($group, 'controllers');
    	//------------/ root [id=1]
    	
    	//------------- user [id=2]
    	$group->id = 2;
    	
    	
    	//------------/ user [id=2]
    	
    	echo "all done";
    	exit;
    }
    
	function index() {
		$this->Review->recursive = 0;
		$this->set('reviews', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid review', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('review', $this->Review->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Review->create();
			if ($this->Review->save($this->data)) {
				$this->Session->setFlash(__('The review has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The review could not be saved. Please, try again.', true));
			}
		}
		$listings = $this->Review->Listing->find('list');
		$categories = $this->Review->Category->find('list');
		$this->set(compact('listings', 'categories'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid review', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Review->save($this->data)) {
				$this->Session->setFlash(__('The review has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The review could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Review->read(null, $id);
		}
		$listings = $this->Review->Listing->find('list');
		$categories = $this->Review->Category->find('list');
		$this->set(compact('listings', 'categories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for review', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Review->delete($id)) {
			$this->Session->setFlash(__('Review deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Review was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function admin_index() {
		$this->disableCache();
		$this->Review->recursive = 0;
		$reviews = $this->paginate('Review');
		foreach ( $reviews as $i_index => $review )
		{
			$listing = $this->Listing->read(null, $review['Review']['listing_id']);
			$review['Review']['listing'] = $listing['Listing']['title'];
			$reviews[$i_index] = $review;
		}
		$this->set('reviews', $reviews);
		$this->disableCache();
	}

	function admin_delete($id = null) {
		$this->disableCache();
		if (!$id) {
			$this->Session->setFlash('Invalid destination');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));
		}
		
		$review = $this->Review->read(null, $id);
		$this->set('review', $review);
		

		if ($this->Review->delete($id)) {
			$this->Session->setFlash('The destination is deleted successfully');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));
		}
			
		$this->Session->setFlash('Error: The review could not be deleted. Please, try again.');
		$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));

		$this->disableCache();
	}
	
	function admin_approve($id = null) {
		$this->disableCache();
		if (!$id) {
			$this->Session->setFlash('Invalid destination');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));
		}
		
		$review = $this->Review->read(null, $id);
		$this->set('review', $review);
		$this->Session->setFlash('Update successfully');
		$this->Review->id    = $id;
		if( $review['Review']['status'] == 1 )
		{
			$this->Review->saveField('status', 0);
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));
		}
		else
		{ 
			$this->Review->saveField('status', 1);
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'reviews', 'action' => 'index'));
		}
		$this->disableCache();
	}
	
}
?>